package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.UpdateProductDescBean;
import com.shop2cn.iapi.agentproducts.service.ImportAgentProductCall;
import com.shop2cn.iapi.agentproducts.service.UpdateProductDescCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*更新商品图文详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_updateProductDesc{
	private static UpdateProductDescBean bean;
	private static UpdateProductDescCall call;
	
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();

	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	
	// 渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	// 代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品图文详情");
	}

	@Before
	public void caseUp() {
		bean=new UpdateProductDescBean();
		call=new UpdateProductDescCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_001() {
		Logger.start(true,"更新商品图文详情--验证正常场-本系统格式的图片");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<Map<String, Object>> maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, sellerId);
			List<Map<String, Object>> pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())>0).collect(Collectors.toList());
					
			for (int i = 0; i < bean.getDescPicList().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(bean.getDescPicList().get(i+1), pictures.get(i).get("sOriUrl").toString(),"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
			String maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, sellerId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.verifyEquals(true, maps1.contains(bean.getDescription()), "验证Description");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_002() {
		Logger.start(true,"更新商品图文详情--验证非本系统格式的图片");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1587720278&di=7a33ea4f0d6be28e160e603f65c4ce55&src=http://cdnsfb.soufunimg.com/viewimage/open/0/2020_4/23/M15/29/833faccf-9830-4b47-a9a4-65f8854cfdac/450x338c.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");
			Logger.verifyEquals(true, call.getMessage1().contains("请使用内部图片进行上传"), "验证message");
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_003() {
		Logger.start(true,"更新商品图文详情--验证图片数等于最大值-30张");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            List<String> pics=new ArrayList<String>();
            for (int i=0;i<30 ;i++) {
            	 pics.add("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667"+i+"_n_w_o.jpg");
			}
            
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(pics);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<Map<String, Object>> maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, sellerId);
			List<Map<String, Object>> pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())>-1).collect(Collectors.toList());
					
			for (int i = 0; i < bean.getDescPicList().size(); i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(bean.getDescPicList().get(i), pictures.get(i).get("PictureUrl").toString(),
						"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
			String maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, sellerId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.verifyEquals(true, maps1.contains(bean.getDescription()), "验证Description");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_004() {
		Logger.start(true,"更新商品图文详情--验证图片数大于最大值-31张");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            List<String> pics=new ArrayList<String>();
            for (int i=0;i<31 ;i++) {
            	 pics.add("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667"+i+"_n_w_o.jpg");
			}
            
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(pics);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_005() {
		Logger.start(true,"更新商品图文详情-验证userId不存在");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId+111);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_006() {
		Logger.start(true,"更新商品图文详情-验证userId为空");
		try {

			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
          //  bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_007() {
		Logger.start(true,"更新商品图文详情-验证productId不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            bean.setUserId(sellerId);
            
            bean.setProductId(productId+111);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();
			Logger.verifyEquals("201", call.getCode1(), "验证code");					
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_008() {
		Logger.start(true,"更新商品图文详情-验证productId为空");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
           // bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_009() {
		Logger.start(true,"更新商品图文详情-验证productId与userId不匹配");
		try {
			ImportAgentProductCall importagentproductCall=new ImportAgentProductCall();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			importagentproductCall.setData(importAgentProductBean);
			importagentproductCall.callService();

			String agentProductId = importagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			
            bean.setUserId(agentUserId1);
            bean.setProductId(productId);
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_010() {
		Logger.start(true,"更新商品图文详情-验证mainPicList为空，没问题");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("详情文字_"+TestDataManager.getRandomIntNum(5));
            //bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_011() {
		Logger.start(true,"更新商品图文详情--description若为null表示更新图片，为空表示清空详情内容");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription(null);
            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<Map<String, Object>> maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, sellerId);
			List<Map<String, Object>> pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())>0).collect(Collectors.toList());
					
			for (int i = 0; i < bean.getDescPicList().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(bean.getDescPicList().get(i+1), pictures.get(i).get("sOriUrl").toString(),"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
			String maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, sellerId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.debug("maps1:"+maps1);
			Logger.verifyEquals(true, maps1.contains(addAgentProductBean.getDescription()), "验证Description");
			
			bean.setDescription("");
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			 maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, sellerId);
			 pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())>0).collect(Collectors.toList());
					
			for (int i = 0; i < bean.getDescPicList().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(bean.getDescPicList().get(i+1), pictures.get(i).get("sOriUrl").toString(),"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, sellerId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.debug("maps1:"+maps1);
			Logger.verifyEquals(true, maps1.contains("[]"), "验证Description");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductDesc_012() {
		Logger.start(true,"更新商品图文详情--DescPicList若为null表示不更新");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setDescription("desc");
//            bean.setDescPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<Map<String, Object>> maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, sellerId);
			List<Map<String, Object>> pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())==0).collect(Collectors.toList());
			Logger.debug("pictures:"+pictures);
			for (int i = 0; i < addAgentProductBean.getDescPicList().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(addAgentProductBean.getDescPicList().get(i+1), pictures.get(i).get("sOriUrl").toString(),"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
			String maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, sellerId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.debug("maps1:"+maps1);
			Logger.verifyEquals(true, maps1.contains(bean.getDescription()), "验证Description");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}